package com.ankiang.treeprint.fragment.components;

import android.view.LayoutInflater;
import android.view.View;

import com.ankiang.treeprint.R;
import com.ankiang.treeprint.base.BaseFragment;
import com.ankiang.treeprint.lib.annotation.Widget;
import com.ankiang.treeprint.manager.QDDataManager;
import com.ankiang.treeprint.model.TRItemNode;
import com.ankiang.treeprint.view.TRConfig;
import com.ankiang.treeprint.view.TRListener;
import com.qmuiteam.qmui.util.QMUIViewHelper;
import com.qmuiteam.qmui.widget.QMUITopBarLayout;
import com.qmuiteam.qmui.widget.grouplist.QMUICommonListItemView;
import com.qmuiteam.qmui.widget.grouplist.QMUIGroupListView;

import java.util.ArrayList;
import java.util.Iterator;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * {@link QMUIGroupListView} 的使用示例。
 * Created by Kayo on 2016/11/21.
 */

@Widget(widgetClass = TRListItemFragment.class, iconRes = R.mipmap.icon_grid_group_list_view)
public class TRListItemFragment extends BaseFragment {

    public static final int LIST_ITEM_CALLNAME = 1; // 显示称呼

    public static final int KEY_DATA = 1;

    @BindView(R.id.topbar)
    QMUITopBarLayout mTopBar;

    @BindView(R.id.groupListView)
    QMUIGroupListView mGroupListView;


    TRListener searchCallBack;

    private QDDataManager.QDItemDescription mQDItemDescription;

    // 保存数据
    private ArrayList mItemList;
    private String mTitle;


    // 配置
    private TRConfig conf;
    private int mOp = LIST_ITEM_CALLNAME;

    // 设置选择还是查看
    public void setOp(int mOp) {
        this.mOp = mOp;
    }

    public void setTitle(String title) {
        this.mTitle = title;
    }

    public void setItemList(ArrayList list) {
        this.mItemList = list;
        if(mItemList == null){
            mItemList = new ArrayList();
        }
    }

    @Override
    protected View onCreateView() {
        View root = LayoutInflater.from(getActivity()).inflate(R.layout.tr_fragment_list_item, null);
        ButterKnife.bind(this, root);

        conf = TRConfig.getInstance();


        mQDItemDescription = QDDataManager.getInstance(getContext()).getDescription(this.getClass());



        initTopBar();

        initGroupListView();

        initListener();

        return root;
    }

    private void initListener() {

    }

    private void showResultList() {

        if (this.mItemList == null) {
            return;
        }


        final View.OnClickListener onClickListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int id = (int) v.getTag();

                if (mOp == LIST_ITEM_CALLNAME) {

                }
            }
        };
        mGroupListView.removeAllViews();

        QMUIGroupListView.Section list = QMUIGroupListView.newSection(getContext());

        String tips = String.format(V(R.string.list_item_total_tips), mItemList.size());
        list.setTitle(tips);
        TRItemNode item;
        QMUICommonListItemView itemView;


        Iterator<TRItemNode> iter = mItemList.iterator();

        while (iter.hasNext()) {
            item = iter.next();
            String callname = item.getCallname();
            String name = item.getName();

            itemView = mGroupListView.createItemView(callname);
            itemView.setTag(item.getId());
            itemView.setOrientation(QMUICommonListItemView.HORIZONTAL);
            if(name != null && !"".equals(name)) {
                itemView.setDetailText(name);
            }

//            Drawable x = conf.getSexImgDrawable(getContext(), item.getSex());
//            itemView.setImageDrawable(x);

            list.addItemView(itemView, onClickListener);
        }
        list.addTo(mGroupListView);
    }

    private void initTopBar() {
        mTopBar.addLeftBackImageButton().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popBackStack();
            }
        });
        mTopBar.setTitle(mTitle);
        mTopBar.addRightTextButton(R.string.cancel, QMUIViewHelper.generateViewId())
                .setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        popBackStack();
                    }
                });
    }


    private void initGroupListView() {
        showResultList();

    }

    public void setSearchCallBack(TRListener searchCallBack) {
        this.searchCallBack = searchCallBack;
    }
}
